package Logic;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import sg.edu.nyp.sit.websvc.ADAuthenticateSoapProxy;

import Database.AuditLogDB;
import Database.CredentialDB;
import Model.Credential;
import Model.Staff;

/**
 * Servlet implementation class Credential_BLL
 */
@WebServlet("/Credential_BLL")
public class Credential_BLL extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Credential_BLL()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("Authenticating credentials");
		boolean authenticateWithWS = false;
		try {
			Context env = (Context)new InitialContext().lookup("java:comp/env");
			if (env.lookup("AuthenticateWithWS").toString().equalsIgnoreCase("No"))
				authenticateWithWS = false;
			else
				authenticateWithWS = true;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  

		// Set username and password
		String userName = request.getParameter("username");
		String passWord = request.getParameter("password");
		Credential cred = new Credential();
		cred = CredentialDB.getCredentials(userName);
		int authResult = -1;
		javax.servlet.http.HttpSession session = request.getSession();
		if (userName!=null && userName.length()>0) {
			if (passWord!=null && passWord.length()>0) {
				
				if (authenticateWithWS) {
					System.out.println("Authenticating with WS");
					ADAuthenticateSoapProxy authenticateWS = new ADAuthenticateSoapProxy();
					authenticateWS.setEndpoint("https://websvc.sit.nyp.edu.sg/SIT_Auth/Authenticate.asmx");
					authResult = authenticateWS.valLogin(userName, passWord);
				}
				else {
					Credential retrCred = CredentialDB.getCredentials(userName);
					if (retrCred!=null && retrCred.getPassword().equals(passWord))
						authResult=0;
				}
//				int authResult = fypjWS.studAuth(userName, passWord);
				System.out.println("Authentication result: " + authResult);

				if (authResult==0) {
					Staff staff = new Staff();
					staff = CredentialDB.getStaffName(userName);
					session.setAttribute("name", staff.getName());
					session.setAttribute("username", userName);
					session.setAttribute("role", cred.getRole());
					
					AuditLogDB.insertLog("login", (String) request
							.getSession().getAttribute("name"));
					
					response.sendRedirect("welcome.jsp");
				}
				else
					response.sendRedirect("login.jsp?error=-1");		
			}
		}
		
		

	}

}
